<?php
/*
 *---------------------------------------------------------------------------
 *
 *                  T E N C E N T   P R O P R I E T A R Y
 *
 *     COPYRIGHT (c)  2008 BY  TENCENT  CORPORATION.  ALL RIGHTS
 *     RESERVED.   NO  PART  OF THIS PROGRAM  OR  PUBLICATION  MAY
 *     BE  REPRODUCED,   TRANSMITTED,   TRANSCRIBED,   STORED  IN  A
 *     RETRIEVAL SYSTEM, OR TRANSLATED INTO ANY LANGUAGE OR COMPUTER
 *     LANGUAGE IN ANY FORM OR BY ANY MEANS, ELECTRONIC, MECHANICAL,
 *     MAGNETIC,  OPTICAL,  CHEMICAL, MANUAL, OR OTHERWISE,  WITHOUT
 *     THE PRIOR WRITTEN PERMISSION OF :
 *
 *                        TENCENT  CORPORATION
 *
 *       Advertising Platform R&D Team, Advertising Platform & Products
 *       Tencent Ltd.
 *---------------------------------------------------------------------------
 */

 /**
  * The class for render view
  *
  * @package lib.core
  * @author  ianzhang <ianzhang@tencent.com>
  * @version TMView.class.php 2008-9-6 by ianzhang
  */
class JYView {
    private $filePath;
    public function __construct($filePath = null) {
        if ($filePath != null) {
            $this->filePath = $filePath;
        }
    }

    /**
     * Get template path
     *
     * @return the template path
     */
    protected function getTemplatePath() {
        $dispatcher = JYDispatcher::createInstance();
        $filePath = $dispatcher->getController() .'/'. $dispatcher->getAction() . '.php';
        return $filePath;
    }
    
    /**
     * Render the template with layout
     *
     * @param string $layout   layout type
     * @param string $tpl     template file
     *
     * @return the response content
     */
    public function render($vars=array(), $tpl=null) {
        if($tpl == null) {
            $tpl = $this->getTemplatePath();
        }
        $view = new JYView();
        return $view->renderFile($vars, $tpl);
    }

    /**
     * Render the file template
     *
     * @param  array  $vars        the change variables array
     * @param  string $filePath    the template file path
     * @return string              render content
     * @throws TMViewException
     */
    public function renderFile($vars, $filePath) {
        foreach($vars as $key => $value) {
            $$key=$value;
        }
        ob_start();
        if(substr($filePath,0,1) != "/" && !strpos($filePath, ":")) {
            $filePath = APP_PATH . 'tpl/' . $filePath;
        }
        if(file_exists($filePath)) {
            require($filePath);
        } else {
        	JYTools::error("No view file!".$filePath);
            exit;
        }
        $result = ob_get_contents();
        @ob_end_clean();
        //header('Content-Length: '.strlen($result));
        return $result;   // 显示页面内容
    }
    
    public function url($controllerName='',$action='',$params=array(),$anchor=''){
    	$dispatcher = JYDispatcher::createInstance();
    	if(!$controllerName) $controllerName = $dispatcher->getController();
    	if(!$action) $action = $dispatcher->getAction();
    	
    	echo JYUrl::rewrite($controllerName,$action,$params,$anchor);
    }
}